/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.zxing.Result;
import com.lenovo.framework.base.InjectableFragment;
import com.lenovo.mso2o.R;

import butterknife.Bind;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

/**
 * Created by hippo on 12/21/15.
 */
public class DecoderFragment extends InjectableFragment implements ZXingScannerView.ResultHandler {
    @Bind(R.id.decoderview)
    ZXingScannerView mScannerView;
    @Bind(R.id.exampleTextView)
    TextView myTextView;
    public static final int DECODER_REQUEST = 0x03f01;
    public static final String DECODER_RES = "decoder_res_text";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public int provideContentRes() {
        return R.layout.decoder_fragment;
    }

    @Override
    protected void onFragmentInVisible(Bundle savedInstanceState) {

    }

    @Override
    protected void onFragmentVisible(Bundle savedInstanceState) {

    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCameraPreview();
        mScannerView.stopCamera();
    }

    @Override
    public void handleResult(Result result) {
        myTextView.setText(result.getText());
        Intent intent = new Intent();
        intent.putExtra(DECODER_RES, result.getText());
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }
}
